<template>
  <!-- 添加或修改场所信息对话框 -->
  <el-dialog :title="title" :visible.sync="dialogVisible" width="1000px" append-to-body :close-on-click-modal="false"
    @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right" class="flex-form"
      size="small">

      <el-form-item label="场所代码" prop="fieldCode">
        <el-input v-model.trim="form.fieldCode" placeholder="请输入场所代码" :disabled="isEdit"/>
      </el-form-item>
      <el-form-item label="场所名称" prop="fieldName">
        <el-input v-model.trim="form.fieldName" placeholder="请输入场所名称" />
      </el-form-item>

      <el-form-item label="关区代码" prop="customsCode">
       <!-- <el-select v-model.trim="form.customsCode" placeholder="请选择关区代码">
         <el-option v-for="dict in selectCustomsList" :key="dict.value" :label="dict.value+'-'+dict.label" :value="dict.value" />
        </el-select>-->
         <select-customs @selectResHandler="selectResHandler"></select-customs>
      </el-form-item>

      <el-form-item label="贸易类型" prop="tradeType">
        <el-select v-model.trim="form.tradeType" placeholder="请选择贸易类型">
          <el-option v-for="dict in dict.type.trade_type_list" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="场所类型" prop="fieldType">
        <el-select v-model.trim="form.fieldType" placeholder="请选择场所类型">
          <el-option v-for="dict in dict.type.field_type" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="进出境" prop="tradeIEFlag">
        <el-select v-model.trim="form.tradeIEFlag" placeholder="请选择进出境">
          <el-option v-for="dict in dict.type.sys_i_e_flag" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="企业代码" prop="entCode">
        <el-input v-model.trim="form.entCode" placeholder="请输入企业代码" />
      </el-form-item>

      <el-form-item label="企业名称" prop="entName">
        <el-input v-model.trim="form.entName" placeholder="请输入企业名称" />
      </el-form-item>

      <el-form-item label="开启收费标志" prop="openChargeMark">
        <el-input v-model.trim="form.openChargeMark" placeholder="请输入开启收费标志" />
      </el-form-item>

      <el-form-item label="卡口道数" prop="gateNum">
        <el-input v-model.trim="form.gateNum" placeholder="请输入卡口道数" />
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import selectCustoms from "@/views/components/base/selectCustoms";
  import {
    getBaseFieldInfo,
    addBaseFieldInfo,
    updateBaseFieldInfo
  } from "@/api/system/baseFieldInfo";
  export default {
    name: "BaseFieldInfo-addEdit",
    components: {
      selectCustoms,
    },
    props: {
      dialogType: {
        type: String,
        default: "add",
      },
      rowData: {
        type: Object,
        default: () => {},
      },
      dict: {}
    },
    data() {
      return {
        isEdit:false,
        // 弹出层
        dialogVisible: false,
        // 标题
        title: "新增",
        // 表单参数
        form: {
          fieldCode: null,
          fieldName: null,
          customsCode: null,
          tradeType: null,
          fieldType: null,
          tradeIEFlag: null,
          entCode: null,
          entName: null,
          openChargeMark: null,
          gateNum: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null

        },
        // 场所信息表格数据
        baseFieldInfoList: [],
        // 表单校验
        rules: {
          fieldCode: [{
            required: true,
            message: "场所代码不能为空",
            trigger: "blur"
          }],
          fieldName: [{
            required: true,
            message: "场所名称不能为空",
            trigger: "blur"
          }],
          customsCode: [{
            required: true,
            message: "关区代码不能为空",
            trigger: "change"
          }],
          tradeType: [{
            required: true,
            message: "贸易类型不能为空",
            trigger: "change"
          }],
          fieldType: [{
            required: true,
            message: "场所类型不能为空",
            trigger: "change"
          }],
          tradeIEFlag: [{
            required: true,
            message: "进出境不能为空",
            trigger: "change"
          }],
          entCode: [{
            required: true,
            message: "企业代码不能为空",
            trigger: "blur"
          }],
          entName: [{
            required: true,
            message: "企业名称不能为空",
            trigger: "blur"
          }],
          gateNum: [{
            required: true,
            message: "卡口道数不能为空",
            trigger: "blur"
          }],
          createBy: [{
            required: true,
            message: "创建人不能为空",
            trigger: "blur"
          }],
          createTime: [{
            required: true,
            message: "创建时间不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    methods: {
      open() {
        this.dialogVisible = true;
        this.$nextTick(() => {
          if (this.dialogType == "add") {
            this.title = "新增";
            this.isEdit=false;
          } else if (this.dialogType == "edit") {
            this.title = "编辑";
            this.loadDetail();
            this.isEdit=true;
          } else if (this.dialogType == "detail") {
            this.title = "详情";
            this.loadDetail();
          }
        });
      },
      close() {
        this.$refs["form"].resetFields();
        this.dialogVisible = false;
      },

      // 获取详情
      loadDetail() {
        const fieldCode = this.rowData.fieldCode;
        getBaseFieldInfo(fieldCode).then((res) => {
          if (res.code == 200) {
            this.form = res.data;
          }
        });
      },
      selectResHandler(selectRes){
        this.form.customsCode=selectRes;
      },

      // 提交表单
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let params = {
              ...this.form,
            };
            if (this.dialogType == "add") {
              addBaseFieldInfo(this.form).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("新增成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            } else {
              params.id = this.rowData.id;
              updateBaseFieldInfo(this.form).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("修改成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            }
          }
        });
      },
    },
  };
</script>

<style lang="scss" scoped></style>
